/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cenluan.user;

import java.util.ArrayList;
import java.util.List;

import com.cenluan.common.OssManager;
import com.cenluan.kit.XssKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;

/**
 * 用户
 * 
 * @author peak
 * 
 */
public class User extends Model<User> {

	private static final long serialVersionUID = 1L;

	public static final User dao = new User();

	private static final String CACHE_NAME = "user";

	private static final String NEWS_CACHE_NAME = "userNews";

	/**
	 * 会员，默认角色
	 */
	public static final String ROLE_MEMBER = "MEMBER";
	/**
	 * 管理员
	 */
	public static final String ROLE_ADMIN = "ADMIN";

	static final List<String> GENDER_LIST = new ArrayList<String>();

	static {
		// gender
		GENDER_LIST.add("男");
		GENDER_LIST.add("女");
		GENDER_LIST.add("未知");
	}

	@Override
	public User findById(Object id) {
		User user = CacheKit.get(CACHE_NAME, id);
		if (user != null) {
			return user;
		}
		user = super.findById(id);
		CacheKit.put(CACHE_NAME, id, user);
		return user;
	}

	@Override
	public boolean update() {
		CacheKit.remove(CACHE_NAME, getStr("id"));
		return super.update();
	}

	/**
	 * 根据小米的用户ID获取用户，即米聊号
	 * 
	 * @param userId
	 * @return
	 */
	public User findByMiUserId(Integer userId) {
		return findFirst("select * from t_user where miUserId=?", userId);
	}

	/**
	 * 根据QQopenid获取用户
	 * 
	 * @param userId
	 * @return
	 */
	public User findByQqOpenId(String qqOpenId) {
		return findFirst("select * from t_user where qqOpenId=?", qqOpenId);
	}

	/**
	 * 依据昵称查找
	 * 
	 * @param nickname
	 * @return
	 */
	public User findByNickname(String nickname) {
		return findFirst("select * from t_user where nickname=?", nickname);
	}

	/**
	 * 创建相册数量
	 * 
	 * @return
	 */
	public long getGalleryCount() {
		return Db.queryLong("select count(*) from t_gallery where createrId=?",
				getStr("id"));
	}

	/**
	 * 上传过的照片数量
	 * 
	 * @return
	 */
	public long getPhotoCount() {
		return Db.queryLong("select count(*) from t_photo where createrId=?",
				getStr("id"));
	}

	/**
	 * 判定是否是管理员
	 * 
	 * @return
	 */
	public boolean isAdmin() {
		return ROLE_ADMIN.equals(getStr("role"));
	}

	/**
	 * 获取管理员列表
	 * 
	 * @return
	 */
	public List<User> findAdminList() {
		return find("select * from t_user where role=?", ROLE_ADMIN);
	}

	/**
	 * 获取html代码，返回和结果是一个链接，文本为用户昵称，地址是用户的主页
	 * 
	 * @return
	 */
	public String getHtml() {
		return "<a href='/user/show/" + getStr("id") + "'>"
				+ XssKit.escapeStr(getStr("nickname")) + "</a>";
	}

	/**
	 * 获取未读消息数量
	 * 
	 * @return
	 */
	public Long getUnreadMessageCount() {
		final String userId = getStr("id");
		return CacheKit.get(NEWS_CACHE_NAME, "UnreadMessageCount:" + userId,
				new IDataLoader() {
					public Object load() {
						return Db.queryLong("select count(*) from t_message "
								+ "where toUserId=? and read=0", userId);
					}
				});
	}

	/**
	 * 获取未读呼叫
	 * 
	 * @return
	 */
	public Long getUnreadCallCount() {
		final String userId = getStr("id");
		return CacheKit.get(NEWS_CACHE_NAME, "UnreadCallCount:" + userId,
				new IDataLoader() {
					public Object load() {
						return Db.queryLong("select count(*) from t_call"
								+ " where toUserId=? and 'read'=0", userId);
					}
				});
	}

	/**
	 * 清除消息通知缓存
	 */
	public void clearNewsCache() {
		String userId = getStr("id");
		CacheKit.remove(NEWS_CACHE_NAME, "UnreadMessageCount:" + userId);
		CacheKit.remove(NEWS_CACHE_NAME, "UnreadCallCount:" + userId);
	}
	
	public String getAvatarUrl(){
		String avatarUrl = getStr("avatarUrl");
		return avatarUrl!=null?avatarUrl:OssManager.getImageUrl("default-avatar.png");
	}

}
